#include <QApplication>
#include <QTime>
#include <QTextStream>
#include <QFileDialog>
#include <QFont>
#include "XWavTab.h"



void XWavTab::tick(qint64 time) {
	QTime curTime, totTime;
	curTime = curTime.addSecs(time / 1000);
	totTime = totTime.addSecs(media->totalTime() / 1000);
	QString curTimeStr = curTime.toString("mm:ss");
	QString totTimeStr = totTime.toString("mm:ss");	
	timeLabel->setText(curTimeStr + " | " + totTimeStr);
	fflush(stdout);
}


void XWavTab::stateChanged(Phonon::State newState, Phonon::State) {
	switch (newState) {
		case Phonon::PlayingState:
			playButton->setText("Pause");
			break;
		case Phonon::StoppedState:
		case Phonon::PausedState:
			playButton->setText("Play");
			break;
		default:
			break;
	}
}


void XWavTab::openButtonClicked() {
	out << "Open Button Clicked!" << endl;
	QString fname = QFileDialog::getOpenFileName(this, tr("Open File"), "",
							 tr("Wav files(*.wav)"));
	QFile f(fname);
	if (f.exists()) {
		fileName = fname;
		Phonon::MediaSource source(fileName);
		media->setCurrentSource(source);
		fileLabel->setText(fileName);
		outputEdit->setText(deleteExt(fileName) + ".lpa");
		wav->update(fileName);
	} else {
		out << "File not found!" << endl;
	}
}


void XWavTab::playButtonClicked() {
	out << "Play Button Clicked!" << endl;
	if (media->state() == Phonon::PlayingState)
		media->pause();
	else
		media->play();
}


void XWavTab::convertButtonClicked() {
	out << "Convert Button Clicked!" << endl;
	wav->convert(outputEdit->text());
}


void XWavTab::setupUI() {	
	QHBoxLayout *tmpLayout;

	// Open & File
	openButton = new QPushButton("Open");
	openButton->setMinimumWidth(60);
	connect(openButton, SIGNAL(clicked()), this, SLOT(openButtonClicked()));
	fileLabel = new QLabel(fileName);
	fileLabel->setFont(QFont("Courier New", 10));
	QHBoxLayout *fileLayout = new QHBoxLayout();
	fileLayout->addWidget(openButton, 1);
	fileLayout->addWidget(fileLabel, 8);
	
	// Play
	playButton = new QPushButton("Play");
	playButton->setMinimumWidth(60);
	connect(playButton, SIGNAL(clicked()), this, SLOT(playButtonClicked()));
	timeLabel = new QLabel("timeLabel");
	timeLabel->setFont(QFont("Courier New", 10));
	seekSlider = new Phonon::SeekSlider(this);
	QHBoxLayout *playLayout = new QHBoxLayout();
	playLayout->addWidget(playButton, 1);
	playLayout->addWidget(seekSlider, 6);
	playLayout->addWidget(timeLabel, 2);

	// Convert
	QVBoxLayout *convertLayout = new QVBoxLayout();

	tmpLayout = new QHBoxLayout();
	outputEdit = new QLineEdit();
	outputEdit->setFont(QFont("Courier New", 10));
	tmpLayout->addWidget(new QLabel("Output: "));
	tmpLayout->addWidget(outputEdit);
	convertLayout->addLayout(tmpLayout);

	tmpLayout = new QHBoxLayout();
	convertButton = new QPushButton("Convert!");
	convertButton->setSizePolicy(QSizePolicy::Expanding,
		QSizePolicy::Expanding);
	connect(convertButton, SIGNAL(clicked()), this, SLOT(convertButtonClicked()));
	tmpLayout->addWidget(wav, 4);
	tmpLayout->addWidget(convertButton, 1);
	convertLayout->addLayout(tmpLayout);

	convertGroup = new QGroupBox("Convert");
	convertGroup->setLayout(convertLayout);
	
	// All
	QVBoxLayout *layout = new QVBoxLayout();
	layout->addLayout(fileLayout, 0);
	layout->addLayout(playLayout, 0);
	layout->addWidget(convertGroup, 1);
	
	setLayout(layout);
}


XWavTab::XWavTab(QWidget *parent) : QWidget(parent) {
	fileName = "../data/1874.wav";
	
	wav = new XWav();
	setupUI();
	
	// initialize audio
	output = new Phonon::AudioOutput(Phonon::MusicCategory, this);
	media = new Phonon::MediaObject(this);
	media->setTickInterval(200);
	tick(0);
	
	Phonon::createPath(media, output);
	
	Phonon::MediaSource source(fileName);
	media->setCurrentSource(source);
	seekSlider->setMediaObject(media);
	outputEdit->setText(deleteExt(fileName) + ".lpa");
	wav->update(fileName);
	//media->play();
	//*/
	// initialize actions
	connect(media, SIGNAL(tick(qint64)), this, SLOT(tick(qint64)));
	connect(media, SIGNAL(stateChanged(Phonon::State, Phonon::State)),
		this, SLOT(stateChanged(Phonon::State, Phonon::State)));
}
